using System;
using System.Collections.Generic;
using UnityEngine;

//Encoding：UTF-8
public class PaiOnTableController : MonoBehaviour
{
    public static PaiOnTableController singleton;

    [Serializable]
    public struct Pai3dList
    {
        public List<Pai3d> pai3ds;
    }

    private class PaiOinTable
    {
        public List<Pai3d> pai3ds;
        public int pai3dIndex;
    }

    private class MingPaiOinTable
    {
        public List<Pai3dList> mingPai3ds;
        public int mingPai3dIndex;
    }

    public GameObject currentPaiEffect;
    public List<Pai3d> player1;
    public List<Pai3d> player2;
    public List<Pai3d> player3;
    public List<Pai3d> player4;
    public List<Pai3dList> player1MingPai;
    public List<Pai3dList> player2MingPai;
    public List<Pai3dList> player3MingPai;
    public List<Pai3dList> player4MingPai;

    private Dictionary<int, PaiOinTable> paiOinTable = new Dictionary<int, PaiOinTable>();
    private Dictionary<int, MingPaiOinTable> mingPaiOinTable = new Dictionary<int, MingPaiOinTable>();

    PaiOnTableController()
    {
        singleton = this;
    }

    private void Awake()
    {
        paiOinTable.Add(0, new PaiOinTable() { pai3ds = player1, pai3dIndex = 0 });
        paiOinTable.Add(1, new PaiOinTable() { pai3ds = player2, pai3dIndex = 0 });
        paiOinTable.Add(2, new PaiOinTable() { pai3ds = player3, pai3dIndex = 0 });
        paiOinTable.Add(3, new PaiOinTable() { pai3ds = player4, pai3dIndex = 0 });
        mingPaiOinTable.Add(0, new MingPaiOinTable() { mingPai3ds = player1MingPai, mingPai3dIndex = 0 });
        mingPaiOinTable.Add(1, new MingPaiOinTable() { mingPai3ds = player2MingPai, mingPai3dIndex = 0 });
        mingPaiOinTable.Add(2, new MingPaiOinTable() { mingPai3ds = player3MingPai, mingPai3dIndex = 0 });
        mingPaiOinTable.Add(3, new MingPaiOinTable() { mingPai3ds = player4MingPai, mingPai3dIndex = 0 });
    }

    public void Init()
    {
        foreach (KeyValuePair<int, PaiOinTable> kv in paiOinTable)
        {
            for (int i = 0; i < kv.Value.pai3ds.Count; ++i)
            {
                kv.Value.pai3ds[i].gameObject.SetActive(false);
            }
            kv.Value.pai3dIndex = 0;
        }
        foreach (KeyValuePair<int, MingPaiOinTable> kv in mingPaiOinTable)
        {
            for (int i = 0; i < kv.Value.mingPai3ds.Count; ++i)
            {
                List<Pai3d> pai3dList = kv.Value.mingPai3ds[i].pai3ds;
                for (int j = 0; j < pai3dList.Count; ++j)
                {
                    pai3dList[j].gameObject.SetActive(false);
                }
            }
            kv.Value.mingPai3dIndex = 0;
        }
        currentPaiEffect.SetActive(false);
    }

    public void ShowPaiOnTable(int playerIndex, int paiID)
    {
        PaiOinTable p = paiOinTable[playerIndex];
        Pai3d showPai = p.pai3ds[p.pai3dIndex];
        showPai.gameObject.SetActive(true);
        showPai.SetPaiType(paiID);
        ++p.pai3dIndex;
        // 标示最新打出的牌
        currentPaiEffect.SetActive(true);
        currentPaiEffect.transform.position = showPai.transform.position;
        currentPaiEffect.transform.rotation = showPai.transform.rotation;
    }

    public void HidePaiOnTable(int playerIndex)
    {
        PaiOinTable p = paiOinTable[playerIndex];
        Pai3d showedPai = p.pai3ds[p.pai3dIndex - 1];
        showedPai.gameObject.SetActive(false);
        --p.pai3dIndex;
        // 最新打出的牌被吃、碰、杠走了，要隐藏效果
        currentPaiEffect.SetActive(false);
    }

    public void ShowMingPaiOnTable(int playerIndex, List<int> paiIDs)
    {
        MingPaiOinTable mp = mingPaiOinTable[playerIndex];
        List<Pai3d> pai3dList = mp.mingPai3ds[mp.mingPai3dIndex].pai3ds;
        for (int i = 0; i < paiIDs.Count; ++i)
        {
            pai3dList[i].gameObject.SetActive(true);
            pai3dList[i].SetPaiType(paiIDs[i]);
        }
        ++mp.mingPai3dIndex;
    }

    public void ShowMingPaiOnTable_AnGang(int playerIndex)
    {
        MingPaiOinTable mp = mingPaiOinTable[playerIndex];
        List<Pai3d> pai3dList = mp.mingPai3ds[mp.mingPai3dIndex].pai3ds;
        for (int i = 0; i < pai3dList.Count; ++i)
        {
            pai3dList[i].gameObject.SetActive(true);
            pai3dList[i].SetPaiType(0); // 0是背面
        }
        ++mp.mingPai3dIndex;
    }

    public void ShowMingPaiOnTable_JiaGang(int playerIndex, int paiID)
    {
        MingPaiOinTable mp = mingPaiOinTable[playerIndex];
        List<Pai3dList> mp3ds = mp.mingPai3ds;
        for (int i = 0; i < mp.mingPai3dIndex; ++i)
        {
            if (mp3ds[i].pai3ds[0].id == paiID)
            {
                mp3ds[i].pai3ds[3].gameObject.SetActive(true);
                mp3ds[i].pai3ds[3].SetPaiType(paiID);
                break;
            }
        }
    }
}
